package com.tuanzi.loan.business.service.galaxycard.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.repository.galaxycard.custom.GalaxyCardAppPromoteStatCustomRepository;
import com.tuanzi.loan.business.service.galaxycard.GalaxyCardAppPromoteStatService;
import com.tuanzi.loan.business.service.galaxycard.GalaxyCardPromoteUserRelateChannelService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.galaxycard.GalaxyCardAppPromoteStatRequest;
import com.tuanzi.loan.vo.response.galaxycard.GalaxyCardAppPromoteStatResponse;

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class GalaxyCardAppPromoteStatServiceImpl implements GalaxyCardAppPromoteStatService{
	
	@Autowired
	private GalaxyCardAppPromoteStatCustomRepository galaxyCardAppPromoteStatCustomRepository;

	@Autowired
	private GalaxyCardPromoteUserRelateChannelService galaxyCardPromoteUserRelateChannelService;

	@Override
	public Page<GalaxyCardAppPromoteStatResponse> list(
			PageSearcher<GalaxyCardAppPromoteStatRequest> pageSearcher) {
		
		//默认查看该用户所有关联渠道的数据，如果在后台输入渠道条件，则只查看该渠道的数据
		List<Integer> channelCodeList = galaxyCardPromoteUserRelateChannelService.findChannelCodeListByUserId(CurrentUser.get().getUserId());
		if(pageSearcher.getSearchCondition().getChannel() != null && channelCodeList.contains(pageSearcher.getSearchCondition().getChannel())){
			channelCodeList.clear();
			channelCodeList.add(pageSearcher.getSearchCondition().getChannel());
		}
		pageSearcher.getSearchCondition().setChannelCodeList(channelCodeList);
		return galaxyCardAppPromoteStatCustomRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
	}

}
